
function meintest() {

    top.TabView.toggle();
}

var TGSwitcher = {};

(function() {
    // Registration
    var namespaces = [];

    this.ns = function(fn) {
        var ns = {};
        namespaces.push(fn, ns);
        return ns;
    };

    // Initialization
    this.initialize = function() {

        for (var i=0; i<namespaces.length; i+=2) {
            var fn = namespaces[i];

            var ns = namespaces[i+1];
            fn.apply(ns);
        }
        
        //TabView._initFrame();
        //window.setTimeout(meintest, 100);
        //window.setTimeout(meintest, 1000);
        //top.TabView.show();
        //top.TabView.hide();

        /*TabView._initFrame(function() {
          let event = document.createEvent("Events");
          event.initEvent("tabviewshow", false, false);
          dispatchEvent(event);

          //let event = document.createEvent("Events");
            
        });*/

        let tabView = TGSwitcher.getTabView();
        //tabView.showTabView();
        //tabView.hideTabView();
        
        if(!tabView && document.getElementById('tgs-toggle')) document.getElementById('tgs-toggle').setAttribute("disabled", true);
    };



    this.getTabView = function() {
        let mediator = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
        let mr_window = mediator.getMostRecentWindow('navigator:browser');
        let tabView = mr_window.document.getElementById('tab-view'); // chrome://browser/content/tabview.html

        return tabView;
    };

    this.popupshowing = function(aPopup) {
        let tabView = this.getTabView();
        let strings = document.getElementById("tgswitcherStrings");
        let notavailable;

        if(aPopup.parentNode.id == "tabview-button") notavailable = document.getElementById("notavail_button");
        if(aPopup.parentNode.id == "tgs-menu") notavailable = document.getElementById("notavail_menu");

        while(aPopup.firstChild && aPopup.firstChild != notavailable) aPopup.removeChild(aPopup.firstChild);
        
        if(!tabView) {
            notavailable.hidden = false;
        }
        else {
            notavailable.hidden = true;
            
            let activeGroupItem = tabView.contentWindow.GroupItems.getActiveGroupItem();
            let groupItems = tabView.contentWindow.GroupItems.groupItems;

            groupItems.forEach(function(groupItem) {
                var menuItem = document.createElement("menuitem");

                var label = groupItem.getTitle();
                if(label) {
                    menuItem.setAttribute("label", label);
                }
                else {
                    menuItem.setAttribute("label", strings.getString('tgs.notitle.label'));
                    //menuItem.setAttribute("class", "notitle");
                }

                if(groupItem == activeGroupItem) menuItem.setAttribute("class", "active");
                
                menuItem.setAttribute("group-id", groupItem.id);
                menuItem.setAttribute("oncommand", "TGSwitcher.setGroup(Number(this.getAttribute('group-id'))); event.stopPropagation();");
                
                aPopup.insertBefore(menuItem, notavailable);
            });
        }

    };
    
    this.setGroup = function(aGroupItem) {
        let tabView = this.getTabView();
        
        //tabView.contentWindow.GroupItems.setActiveGroupItem(tabView.contentWindow.GroupItems.groupItems[groupItem]);
        //tabView.contentWindow.UI.setActiveTab(1);
        //alert(tabView.contentWindow.GroupItems.updateActiveGroupItemAndTabBar);

        //alert(tabView.contentWindow.GroupItems.getActiveGroupItem().id);
        
        let currentGroupItem = tabView.contentWindow.GroupItems.getActiveGroupItem();
        let activateGroupItem = tabView.contentWindow.GroupItems.groupItem(aGroupItem);

        //alert(currentGroupItem._children.length);
        //alert(activateGroupItem._children.length);

        //activateGroupItem.setActiveTab(activateGroupItem._children[0]);

        //tabView.contentWindow.GroupItems.setActiveGroupItem(activateGroupItem);
        //tabView.contentWindow.GroupItems._updateTabBar();

        window.gBrowser.selectedTab = activateGroupItem._children[0].tab;
        //alert(activateGroupItem._children[0]);
        //alert(tabView.contentWindow.GroupItems.getNextGroupItemTab());
        //tabView.contentWindow.GroupItems.setActiveGroupItem(activateGroupItem._children[0]);


        //tabView.contentWindow.GroupItems.updateActiveGroupItemAndTabBar(tabView.contentWindow.GroupItems.getNextGroupItemTab());
        

        //activateGroupItem.childHit(activateGroupItem._children[0]);


        //alert(groupItem._children.length);
        /*groupItem._children.forEach(function(child) {
            //alert(child);
            //tabView.contentWindow.GroupItems.updateActiveGroupItemAndTabBar(child);
        });*/

        
        //let items = tabView.contentWindow.TabItems.getItems();
    };

    this.nextTabGroup = function() {
        let tabView = this.getTabView();
        let tabItem = tabView.contentWindow.GroupItems.getNextGroupItemTab();
        
        if (tabItem) {
            window.gBrowser.selectedTab = tabItem.tab;
        }
    };
    
    this.checkExistingGroups = function(aElem) {
        if(aElem.parentNode.getAttribute("groups") >= 2) aElem.parentNode.setAttribute("disabled", false);
        else aElem.parentNode.setAttribute("disabled", true);
    };
    
    
    

    // Clean up
    this.shutdown = function() {
        window.removeEventListener("load", TGSwitcher.initialize, false);

        window.removeEventListener("unload", TGSwitcher.shutdown, false);
    };

    // Register handlers to maintain extension life cycle.
    window.addEventListener("load", TGSwitcher.initialize, false);

    window.addEventListener("unload", TGSwitcher.shutdown, false);
    
}).apply(TGSwitcher);